package UiUtil;


import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.openqa.jetty.html.Break;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class ReadTestCase {
public String Filename;
public static String Url=null,PageValidationTitle=null,TcName=null,XlName=null;
static BrowserSetup Bs;
public static int err=0;

public ReadTestCase(String filename,String Xlname) {
	
	Filename = filename;
	XlName=Xlname;
	readXml(Filename);
	
}

public static void readXml(String fpath)
{
Document XMLDocument = parse(fpath);
/*
*Assigning root elements from the template XML to the roorElementObj
*/

NodeList step = XMLDocument.getElementsByTagName("Step");
NodeList nl = XMLDocument.getElementsByTagName("TestCase");
//getParamattrib(nl);
String name,value;
int surl=0,tcStatus=0;
for (int i = 0; i < nl.getLength(); i++) {
	if(nl.item(i).getAttributes()!=null){
		//System.out.println("KKKATTTTRTRTRTRTRTRTRTRTRTRT PRESENENENENEEENEENEENEN");
		//NodeList childn1=nl.item(i).getChildNodes();
		NamedNodeMap namedNodeMap = nl.item(i).getAttributes();
		for(int j=0;j<namedNodeMap.getLength();j++){
			AttribObj attribObj = new AttribObj();
			attribObj.setField(namedNodeMap.item(j).getNodeName());
			attribObj.setValue(namedNodeMap.item(j).getNodeValue());
			name=attribObj.getField();
			value=attribObj.getValue();
			
			//value=nl.item(i).getTextContent();
			
			
			if(name.equalsIgnoreCase("Url"))
			{
				if(value=="" || value== " ")
				{
					Url="Apllication.Url";
					System.out.print(Url);
					System.out.println(" "+value);
					surl=1;
					
				}else
				{
					
					Url=value;
					System.out.print(attribObj.getValue());
					System.out.println(" "+Url);
					surl=2;
				}
				
									
			}
			else
			{
				System.out.println("TestCase"+name+": "+value);
				TcName=value;
			}
			
		}if (surl==0)
		{
			err=1;
			System.out.println("Error - Application Url is missing ");
		}
		
	}
	}
File directory = new File (".");
String path=null,Status;

try {
	System.out.println(directory.getCanonicalPath());
	path = directory.getCanonicalPath()+File.separator+"src"+File.separator+"lib"+File.separator+"chromedriver.exe";
	System.out.println(path);
} catch (IOException e1) {
	// TODO Auto-generated catch block
	e1.printStackTrace();
}
Bs=new BrowserSetup(path,"Chrome",Url);
	for (int i = 0; i < step.getLength(); i++) {
	if(step.item(i).getAttributes()!=null){
		//System.out.println("KKKATTTTRTRTRTRTRTRTRTRTRTRT PRESENENENENEEENEENEENEN");
		//NodeList childn1=nl.item(i).getChildNodes();
		NamedNodeMap namedNodeMap = step.item(i).getAttributes();
		for(int j=0;j<namedNodeMap.getLength();j++){
			AttribObj attribObj = new AttribObj();
			attribObj.setField(namedNodeMap.item(j).getNodeName());
			attribObj.setValue(namedNodeMap.item(j).getNodeValue());
			name=attribObj.getField();
			value=attribObj.getValue();
			System.out.println("Step - "+name+": "+value);
			
		}
		NodeList childn1=step.item(i).getChildNodes();
		
	/*for(int i1 = 0;i1<childn1.getLength();i1++)
		{*/
		Status=getParamattrib(childn1);
		//}
		if(Status.equalsIgnoreCase("failed")){
			System.out.println(Status+"Skipping Remaining Steps");
			tcStatus=1;
			break;
		}
		
		
	}
	}
	if(tcStatus==1)
	{
		System.out.println("Test Case Failed");
	}
	Bs.exitTestcase();
	
}

public static String  getParamattrib(NodeList ParamAttr)
{
	 String FrameSrc= null,FindElementById = null,FindElementByName= null,FindElementByXpath= null,Action = null,Keyvalue= null;
	//NodeList ParamAttr = XMLDocument.getElementsByTagName("param");
	
	int flag=0,sframe=0,saction=0;
	for (int i = 0; i < ParamAttr.getLength(); i++) {
		
		if(ParamAttr.item(i).getAttributes()!=null){
			String value=null,event=null,eventValue=null;
			//System.out.println("KKKATTTTRTRTRTRTRTRTRTRTRTRT PRESENENENENEEENEENEENEN");
			NamedNodeMap namedNodeMap = ParamAttr.item(i).getAttributes();
			//System.out.println(ParamAttr.item(i).getTextContent());
			
			for(int j=0;j<namedNodeMap.getLength();j++){
				AttribObj attribObj = new AttribObj();
				attribObj.setField(namedNodeMap.item(j).getNodeName());
				attribObj.setValue(namedNodeMap.item(j).getNodeValue());
				value=ParamAttr.item(i).getTextContent();
				
				String attribute =attribObj.getValue();
				//System.out.println(attribute);
				//System.out.print("Parameter "+attribObj.getField()+" : ");
				/*if(attribute.equalsIgnoreCase("Url"))
				{
					if(value=="" || value== " ")
					{
						Url="Apllication.Url";
						System.out.print(Url);
						System.out.println(" "+value);
						
						surl=1;
					}else
					{
						
						Url=value;
						System.out.print(attribObj.getValue());
						System.out.println(" "+Url);
						surl=2;
					}
					
										
				}
				else*/
					if (attribute.equalsIgnoreCase("FrameSrc"))
				{
					
					if(value=="" || value== " ")
					{
						System.out.println(attribute+" is optional in this case");	
						sframe=1;
					}else
					{
						FrameSrc=value;
						System.out.print(attribObj.getValue());
						System.out.println(" "+FrameSrc);
						sframe=2;
					}
					
				}
				else if (attribute.equalsIgnoreCase("FindElementById"))
				{
					if(value=="" || value== " ")
					{
						flag++;						
					}else
					{
						FindElementById=value;
						System.out.print(attribObj.getValue());
						System.out.println(" "+FindElementById);
						
					}
					
				}
				else if (attribute.equalsIgnoreCase("FindElementByName"))
				{
					if(value=="" || value== " ")
					{
						flag++;						
					}else
					{
						FindElementByName=value;
						System.out.print(attribObj.getValue());
						System.out.println(" "+FindElementByName);
					}
					
				}
				else if (attribute.equalsIgnoreCase("FindElementByXpath"))
				{
					if(value=="" || value== " ")
					{
						flag++;						
					}else
					{
						FindElementByXpath=value;
						System.out.print(attribObj.getValue());
						System.out.println(" "+FindElementByXpath);
					}
					
				}
				else if (attribute.equalsIgnoreCase("Action"))
				{
					j++;
					event=namedNodeMap.item(j).getNodeName();
					if(event.equalsIgnoreCase("event"))
					{
						eventValue=namedNodeMap.item(j).getNodeValue();	
					}
					
					
					
					
					if(eventValue=="" || eventValue== " ")
					{
						saction=1;					
					}else  
					{
						
						Action=eventValue;
						System.out.print(attribObj.getValue());
						System.out.println(" "+Action);
						saction=2;
						//j++;
						if(eventValue.equalsIgnoreCase("sendKey"))
						{
							
							
							/*String Key;
							Key=namedNodeMap.item(j).getNodeName();*/
							if(value!="")
							{
								Keyvalue=value;
								
								/*System.out.print(Key);
								System.out.println(" "+Keyvalue);
								*/
							}
							else
							{
								saction=0;
							}
								
						}
						else if(eventValue.equalsIgnoreCase("getinfo"))
						{
							PageValidationTitle=value;
							sframe=1;
							saction=2;
							flag=0;
						}
						else if(eventValue.equalsIgnoreCase("Validate"))
						{
							PageValidationTitle=value;
							sframe=1;
							saction=2;
							flag=0;
						}
							
					}
					
				}
				else 
				{			err=1;	
						System.out.println(" Invalid TestCase  ");
					
					
				}
				
				//System.out.println(ParamAttr.item(i).getTextContent());
				//System.out.println(attribObj.getField());
				/*System.out.println(attribObj.getField());
				System.out.println(attribObj.getValue());*/
			}	
			
			
		}
			
			
		
	}
	
	 if (sframe==0)
	{
		 err=1;
		System.out.println("Warning - Frame is missing ");
	}
	if (saction!=2)
	{
		err=1;
		System.out.println("Error - Action / event value is missing ");
	}
	 if(flag==3)
	{
		 err=1;
		System.out.println("Error - Provide any one of the following attribute FindElementById / FindElementByName / FindElementByXpath");	
	}
	 if(err==0)
	 {
		 //System.out.println("---");
		 //System.out.println(FrameSrc+"\n"+FindElementById+"\n"+FindElementByName+"\n"+FindElementByXpath+"\n"+Action+"\n"+Keyvalue);
		// System.out.println("---");	
			String Result=Bs.launchStep(FrameSrc,FindElementById,FindElementByName,FindElementByXpath,Action,Keyvalue); 
			//System.out.println("---"+Result+"--");
			return Result;
			
	 }
	 else
	 {
		// System.out.println("--Error in TestCase--");
		 return "Failed";
		 
	 }
	
}

public static Document parse(String fileName) {
	Document document = null;
	// Initiate DocumentBuilderFactory
	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

	// To get a validating parser
	factory.setValidating(false);
	// To get one that understands namespaces
	factory.setNamespaceAware(false);

	try {
		// Get DocumentBuilder
		DocumentBuilder builder = factory.newDocumentBuilder();
		// Parse and load into memory the Document
		document = builder.parse(new File(fileName));

		return document;

	} catch (SAXParseException spe) {
		// Error generated by the parser
		System.out.println("\n** Parsing error , line "
				+ spe.getLineNumber() + ", uri " + spe.getSystemId());
		System.out.println(" " + spe.getMessage());
		// Use the contained exception, if any
		Exception x = spe;
		if (spe.getException() != null)
			x = spe.getException();
		x.printStackTrace();
	} catch (SAXException sxe) {
		// Error generated during parsing
		Exception x = sxe;
		if (sxe.getException() != null)
			x = sxe.getException();
		x.printStackTrace();
	} catch (ParserConfigurationException pce) {
		// Parser with specified options can't be built
		pce.printStackTrace();
	} catch (IOException ioe) {
		// I/O error
		ioe.printStackTrace();
	}

	return null;
}
public static void main(String [] a)
{
	ReadTestCase tc=new ReadTestCase(a[0],a[1]);
	
}
	
	
	
}
